package com.atomscat.lottery.entity.db;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.AbstractAuditable;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.lang.Nullable;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Optional;

@Setter
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U, PK extends Serializable> extends AbstractPersistable<PK> {

    @Id
    @GeneratedValue
    @Nullable
    private PK id;

    @ManyToOne
    @Nullable
    @CreatedBy
    private U createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Nullable
    @CreatedDate
    private Date createdDate;

    @ManyToOne
    @Nullable
    @LastModifiedBy
    private U lastModifiedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Nullable
    @LastModifiedDate
    private Date lastModifiedDate;

    @JsonIgnore
    public Optional<U> getCreatedBy() {
        return Optional.ofNullable(this.createdBy);
    }

    @JsonIgnore
    public Optional<U> getLastModifiedBy() {
        return Optional.ofNullable(this.lastModifiedBy);
    }
}
